package com.wanshi.servlet;

import com.alibaba.fastjson.JSON;
import com.wanshi.entity.Order;
import com.wanshi.entity.User;
import com.wanshi.service.OrderService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet("/order/orderList")
public class orderList extends HttpServlet {

    private OrderService orderService = new OrderService();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //1 根据用户id查询  该用户所有订单  List
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");

        List<Order> orders = orderService.findOrdersByUser(user.getId());


        //2 存入request , 请求转发到jsp进行展示
        //response.getWriter().write(JSON.toJSONString(orders));
        request.setAttribute("list",orders);
        request.getRequestDispatcher("/order/orderList.jsp").forward(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}

